<!DOCTYPE html>
<html>
<head>
    <title>递归</title>
</head>
<body>
</body>
<script type="text/javascript">
    var menuList = [
        {
            id: 0,
            name: '首页',
            icon: '<i class="anticon anticon-home"></i>',
            link: '/home',
            child: [
                {id: 0, name: '仪表盘', link: '/home/dashboard/index'},
            ]
        },
        {
            id: 1,
            name: '系统管理',
            icon: '<i class="anticon anticon-setting"></i>',
            link: '/system',
            child: [
                {
                    id: 0, name: '用户管理', link: '/system/user/list',
                    child: [
                        {id: 0, name: '用户管理a', link: '/system/user/list/a'},
                        {
                            id: 1, name: '角色管理b', link: '/system/user/list/b', child: [
                                {id: 0, name: '用户管理b1', link: '/system/user/list/b/b1'},
                                {id: 1, name: '角色管理b2', link: '/system/user/list/b/b2'}
                            ]
                        }
                    ]
                },
                {
                    id: 1, name: '角色管理', link: '/system/role', child: [
                        {
                            id: 0, name: '角色列表', link: '/system/role/list', child: [
                                {id: 0, name: '角色列表a', link: '/system/role/list/a'}
                            ]
                        }
                    ]
                }
            ]
        }
    ];
    var getUrlTree = function (list) {
        var p = '';
        var urlList = [];
        for (var i = 1; i < list.length; i++) {
            if (urlList.length === 0) {
                urlList.push('/' + list[i]);
                p = '/' + list[i];
            } else {
                p = p + '/' + list[i];
                // if (i >= 3) {
                urlList.push(p);
                // }
            }
        }
        return urlList;
    };
    var getRouterTree = function (url, router) {
        var treeList = [];

        function currentFn(url, router) {
            for (var i = 0; i < url.length; i++) {
                for (var j = 0; j < router.length; j++) {
                    if (url[i] === router[j].link) {
                        treeList.push(router[j]);
                        if (router[j].child && router[j].child.length > 0) {
                            currentFn(url, router[j].child);
                        }
                    }
                }
            }
        }

        currentFn(url, router);
        return treeList;
    };
    console.log(getUrlTree('/system/user/list'.split('/')));
    /*
    * /system/user/list/b/b1不得包含?key=value
    * */
    console.table(getRouterTree(getUrlTree('/system/role/list/a'.split('/')), menuList));

</script>
</html>